package one.stand.controller;

import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.CardSaveRequest;
import com.jmp.base.dto.SmsCaptchaCheckRequest;
import com.jmp.base.dto.SmsCodeEnums;
import com.jmp.feign.CardFeign;
import com.jmp.feign.SmsFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.UserCardRequest;
import one.stand.dto.UserDto;
import one.stand.dto.UserPhoneRequest;
import one.stand.model.ResultModel;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContextHolder;
import one.stand.util.Result;
import one.stand.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SmsFeign smsFeign;
    @Autowired
    private CardFeign cardFeign;
    @Autowired
    private UserAdminService userAdminService;

    @ApiOperation("绑定手机号")
    @PostMapping("/phone")
    @SecurityAuthCheck
    public Result phone(@RequestBody UserPhoneRequest request) {
        AssertUtil.isTrue(smsFeign.captchaCheck(SmsCaptchaCheckRequest.builder()
                .phone(request.getPhone())
                .type(SmsCodeEnums.login_sms.getCode())
                .captcha(request.getSmsCode())
                .build()).getData().isSuccess(), "验证码错误");

        Integer userId = RequestContextHolder.getRequestContext().getUser().getUserId();
        userAdminService.updatePhoneGzh(userId, request.getPhone());

        return ResultUtil.success();
    }

    @ApiOperation("生成名片")
    @PostMapping("/card")
    @SecurityAuthCheck
    public Result card(@RequestBody UserCardRequest request) {
        AssertUtil.isTrue(smsFeign.captchaCheck(SmsCaptchaCheckRequest.builder()
                .phone(request.getPhone())
                .type(SmsCodeEnums.login_sms.getCode())
                .captcha(request.getSmsCode())
                .build()).getData().isSuccess(), "验证码错误");

        Integer userId = RequestContextHolder.getRequestContext().getUser().getUserId();
        UserDto user = userAdminService.get(userId);

        CardSaveRequest cardRequest = CardSaveRequest.builder()
                .companyId(request.getCompanyId())
                .password(request.getBossPwd())
                .name(user.getName())
                .phone(request.getPhone())
                .style(0)//0 名片样式
                .id(userId)
                .token(RequestContextHolder.getRequestContext().getRequest().getToken())
                .build();
        ResultModel resultModel = cardFeign.save(cardRequest);
        AssertUtil.isTrue(resultModel);

        return ResultUtil.success();
    }
}
